/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.user.client.ui.nojs;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;

/**
 * A simple popup panel for showing simple message.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 * @deprecated experimatal
 */
public class Popover {

  /**
   * The type for popover.
   */
  public enum Type {
    BOTTOM("bottom"), LEFT("left"), RIGHT("right"), TOP("top");

    String name;

    /**
     * Defines a popover type by the specified name.
     */
    Type(String name) {
      this.name = name;
    }
  }

  /**
   * The popup panel impl for popover.
   */
  private class PopupImpl extends PopupPanel {
    private final Type type;

    public PopupImpl(Type type, String title, Widget widget) {
      super(true, false);
      this.type = type;

      if (title != null) {
        Element header = Document.get().createDivElement();
        header.setInnerHTML(title);
        header.setClassName(TITLE_STYLE);
        this.getContainerElement().appendChild(header);
      }

      this.setStyleName(DEFAUL_STYLENAME);
      this.setWidget(widget);
      this.setType(type);
      // override the display property without changing bootstrap.css
      this.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);
      widget.setStyleName(CONTENT_STYLE);
    }

    /**
     * Positions the popup, called after the offset width and height of the popup are known.
     * 
     * @param relativeObject the ui object to position relative to
     * @param offsetWidth the drop down's offset width
     * @param offsetHeight the drop down's offset height
     */
    private void position(final UIObject relativeObject, int offsetWidth, int offsetHeight) {
      // Calculate left position for the popup. The computation for
      // the left position is bidi-sensitive.

      int textBoxOffsetWidth = relativeObject.getOffsetWidth();
      int textBoxOffsetHeight = relativeObject.getOffsetHeight();
      // Compute the difference between the popup's width and the
      // textbox's width
      int offsetWidthDiff = offsetWidth - textBoxOffsetWidth;
      int offsetHeightDiff = offsetHeight - textBoxOffsetHeight;
      int left;
      int top;
      switch (type) {
        case LEFT:
          left = relativeObject.getAbsoluteLeft() - offsetWidth;
          top = relativeObject.getAbsoluteTop() - (offsetHeightDiff >> 1);
          break;
        case RIGHT:
          left = relativeObject.getAbsoluteLeft() + textBoxOffsetWidth;
          top = relativeObject.getAbsoluteTop() - (offsetHeightDiff >> 1);
          break;
        case TOP:
          left = relativeObject.getAbsoluteLeft() - (offsetWidthDiff >> 1);
          top = relativeObject.getAbsoluteTop() - offsetHeight;
          break;
        default:
          left = relativeObject.getAbsoluteLeft() - (offsetWidthDiff >> 1);
          top = relativeObject.getAbsoluteTop() + textBoxOffsetHeight;
          break;
      }

      setPopupPosition(left, top);
    }

    private void setType(Type type) {
      Element arrow = Document.get().createDivElement();
      this.getElement().appendChild(arrow);
      arrow.setClassName(ARROW_STYLE);

      if (this.type != null) {
        this.getElement().removeClassName(this.type.name);
      }

      this.getElement().addClassName(type.name);
    }
  }

  private static final String DEFAUL_STYLENAME = "popover";
  private static final String ARROW_STYLE = "arrow";
  private static final String CONTENT_STYLE = "popover-content";
  private static final String TITLE_STYLE = "popover-title";

  private PopupImpl popup;

  /**
   * Creates a popover with the give 'type', 'title' and message.
   * 
   * @param type the type in {@link Type#LEFT}, {@link Type#RIGHT}, {@link Type#TOP},
   *          {@link Type#BOTTOM}.
   * @param title the title
   * @param message the message
   */
  public Popover(Type type, String title, String message) {
    this.popup = new PopupImpl(type, title, new HTML(message));
  }

  /**
   * Creates a popover with the give 'type', 'title' and content widget.
   * 
   * @param type the type in {@link Type#LEFT}, {@link Type#RIGHT}, {@link Type#TOP},
   *          {@link Type#BOTTOM}.
   * @param title the title
   * @param widget the content widget
   */
  public Popover(Type type, String title, Widget widget) {
    this.popup = new PopupImpl(type, title, widget);
  }

  /**
   * Shows the popup.
   * 
   * @see PopupPanel#center()
   */
  public void show() {
    popup.center();
  }

  /**
   * Hides the popup.
   * 
   * @see PopupPanel#hide()
   */
  public void hide() {
    popup.hide();
  }

  /**
   * Shows the popover relative to the given target.
   * 
   * @param target the relative target
   */
  public void showRelativeTo(final UIObject target) {
    // Set the position of the popup before it is shown.
    popup.setPopupPositionAndShow(new PositionCallback() {
      @Override
      public void setPosition(int offsetWidth, int offsetHeight) {
        popup.position(target, offsetWidth, offsetHeight);
      }
    });
  }
}
